#include <vector>
using namespace std;
struct TreeNode {
  int val;
  TreeNode *left;
  TreeNode *right;
  TreeNode()
      : val(0), left(nullptr), right(nullptr){} * TreeNode(int x)
      : val(x),
        left(nullptr), right(nullptr) {}
  TreeNode(int x, TreeNode *left, TreeNode *right)
      : val(x), left(left), right(right) {}
};

void dfs(TreeNode *root, vector<int> &res, int layer) {
  if (root == nullptr) {
    return;
  }
  // 该层没有元素被读取
  if (res.size() < layer) {
    res.push_back(root->val);
  }
  dfs(root->right, res, layer + 1);
  dfs(root->left, res, layer + 1);
}

vector<int> rightSideView(TreeNode *root) {
  vector<int> res;
  dfs(root, res, 1);
  return res;
}